package satellite.nav.utils;

/**
 * @Program : satellite-nav
 * @ClassName : GpstTimeUtils
 * @Description :
 * @Date: 2021-12-15 14:04
 * @Version : 1.0
 */
public class GpsTimeUtils {
    //gps时间从1980-1-6 0:0:0
    public static int[] gpst0 = new int[]{1980, 1, 6, 0, 0, 0}; /* gps time reference */

    public static Integer SECONDS_IN_DAY=86400;

    public static Integer SECONDS_IN_WEEK=604800;

    public static GpsTime  utcToGpsTime(int year, int month , int day, int hour, int min, int sec){
        GTime g0 =toGtime(gpst0[0],gpst0[1],gpst0[2],gpst0[3],gpst0[4],gpst0[5]);

        GTime gCurrent=toGtime(year,month,day,hour,min,sec);
        if(g0 != null && gCurrent != null) {
            long second = gCurrent.getTime() - g0.getTime();
            int w = (int) (second / (86400 * 7));
            GpsTime gpsTime = new GpsTime();
            gpsTime.setWeeks(w);
            gpsTime.setSeconds((sec - (double) w * 86400 * 7) + gCurrent.getSec());
        }
        return  null;
    }

    public static GTime toGtime(int year,int month ,int day ,int hour,int min ,int sec){
        int[] doy = new int[]{1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
        //gtime_t time={0};
        GTime time = new GTime();
        int days, secs;
        if (year < 1970 || 2099 < year || month < 1 || 12 < month) {
            return null;
        }
        /* leap year if year%4==0 in 1901-2099 */
        days = (year - 1970) * 365 + (year - 1969) / 4 + doy[month - 1] + day - 2 + (year % 4 == 0 && month >= 3 ? 1 : 0);
        secs = (int) Math.floor(sec);
        time.setTime(new Long(days * 86400 + hour * 3600 + min * 60 + secs));
        time.setSec(new Double(sec-secs));
        return time;
    }

}
